// reducer决定了如何改变state
// reducer是唯一可以触碰store的元素，初始值为initialState，形式上就是一个简单的switch语句，
// 但是注意不能直接改变state，因为state是immutable（不变的）。也就是说我们不能直接使用.pop() or .push()这些方法操作数组

// import {setUserInfo, setStorage} from "@/common/auth";
import *  as actionType from './actionType'

/**
 * @roomInfo Object
 * @roomId 房间ID
 * @contacts 对方
 * @mine 自己
 * @chattingMsg 聊天记录
 * @hasRead      是否已读
 * @isTop        是否置顶
 * @notDisturb   是否勿扰
 * @msgType 消息类型：0 个人；1：群聊
 * */
const initialState = {
  userInfo: {},
  roomInfo: {}
}

export default (state = initialState, action = {}) => {
  switch (action.type) {
    case actionType.USERINFO:
      // setUserInfo(action.userInfo);
      return {
        ...state,
        userInfo: action.userInfo
      }
    case actionType.ROOMINFO:
      // setStorage('roomInfo', action.roomInfo);
      const {roomId} = action.roomInfo;
      return {
        ...state,
        roomInfo: {
          ...state.roomInfo,
          [roomId]: action.roomInfo
        }
      }
    default:
      return state;
  }
}